package cn.edu.lingnan.util;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.HashMap;

public class XmlHandler extends DefaultHandler {

    private HashMap<String,String> hm=new HashMap<>();
    private StringBuffer stringBuffer=new StringBuffer();

    public HashMap<String, String> getHm() {
        return hm;
    }


    @Override
    public void startElement (String uri, String localName,
                              String qName, Attributes attributes)
            throws SAXException
    {//这段代码是Java语言中的一段代码，用于删除StringBuffer对象中的所有字符。
        stringBuffer.delete(0,stringBuffer.length());
    }



    @Override
    public void endElement (String uri, String localName, String qName)
            throws SAXException
    {
        hm.put(qName.toLowerCase(),stringBuffer.toString().trim());
    }


    //读取字符数据时执行，要把读到的数据存取起来
    @Override
    public void characters (char ch[], int start, int length)
            throws SAXException
    {
        stringBuffer.append(ch,start,length);
    }

}
